////
////  RRWebShowVC.swift
////  TodayNews
////
////  Created by ZhangBo on 2019/6/17.
////  Copyright © 2019 ZhangBo. All rights reserved.
////
//
//import UIKit
//import WebKit
//import WebViewJavascriptBridge
//
//extension RRWebShowVC: Routable {
//    var umName: String {
//        return "Web页面 - \(webParam.title)"
//    }
//
//    static func initWithParams(params: RouterParameter?) -> UIViewController? {
//        guard let webParam = params?["webParam"] as? RRWebParam else {
//            d_print("error init 'RRCoinDetailVC' by illegal params")
//            return nil
//        }
//        let webVC = RRWebShowVC.init(param: webParam)
//        return webVC
//    }
//}
//
//final class RRWebShowVC: BaseViewController {
//
//    private var webParam: RRWebParam
//
//    init(param: RRWebParam) {
//        self.webParam = param
//        super.init(nibName: nil, bundle: nil)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//
//    private var webView: LYWKWebView!
//    private var bridge: WKWebViewJavascriptBridge!
//    private var progressView: UIProgressView!
//
//    // MARK: view cycle
//    override func viewDidLoad() {
//        super.viewDidLoad()
//        self.p_initSubviews()
//    }
//
//    deinit {
//        webView.removeObserver(self, forKeyPath: "canGoBack")
//        webView.removeObserver(self, forKeyPath: "estimatedProgress")
//    }
//
//    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
//        if keyPath == "canGoBack" {
//            if webView.canGoBack {
//                self.fd_interactivePopDisabled = true
//            } else {
//                self.fd_interactivePopDisabled = false
//            }
//        } else if keyPath == "estimatedProgress" {
//            progressView.alpha = CGFloat(1.0 - webView.estimatedProgress) + 0.2
//            let progerss = max(Float(webView.estimatedProgress), 0.1)
//            progressView.setProgress(progerss, animated: true)
//            if webView.estimatedProgress >= 1.0 {
//                progressView.alpha = 0.0
//                progressView.setProgress(0.0, animated: false)
//            }
//        } else {
//            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
//        }
//    }
//}
//
//// MARK: - ********* Swift - JS interactive
//extension RRWebShowVC {
//
//}
//
//// MARK: - ********* webview delegate
//extension RRWebShowVC: WKNavigationDelegate {
//
//    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//
//    }
//
//    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
//        d_print("==== error: \(error)")
//    }
//
//    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
//        d_print("==== error: \(error)")
//    }
//
//    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
//        if (navigationAction.targetFrame == nil) {
//            webView.load(navigationAction.request)
//        }
//        guard let policy = WKNavigationActionPolicy(rawValue: WKNavigationActionPolicy.allow.rawValue + 2)
//            else {
//            decisionHandler(.allow);
//            return
//        }
//        decisionHandler(policy)
//    }
//}
//
//// MARK: - ********* UIResponse Funcs
//extension RRWebShowVC {
//    override func p_actionLeft() {
//        if webView.canGoBack {
//            webView.goBack()
//        } else {
//            self.ly.close()
//        }
//    }
//
//    override func p_actionRight() {
//        d_print(#line)
//        bridge.callHandler("pushToNewWebSite")
//    }
//}
//
//// MARK: - ********* UI init
//extension RRWebShowVC {
//    fileprivate func p_initSubviews() {
//
//        var webY: CGFloat = 0
//        if webParam.needNav {
//            self.navigationItem.title = webParam.title
//            webY = kNavBottom()
//        } else {
//            self.fd_prefersNavigationBarHidden = true
//        }
//
//        webView = LYWKWebView(frame: CGRect.init(x: 0, y: webY, width: kScreenWid(), height: kScreenHei() - webY))
//        webView.navigationDelegate = self
//        webView.uiDelegate = self
//        webView.allowsBackForwardNavigationGestures = true
//        self.view.addSubview(webView)
//        /// 进度条
//        progressView = UIProgressView(progressViewStyle: .default)
//        progressView.frame = CGRect(x: 0, y: 0, width: webView.width, height: 4)
//        progressView.trackTintColor = UIColor.clear
//        progressView.progressTintColor = kThemeColor()
//        progressView.isHidden = false
//        webView.addSubview(progressView)
//
//        /// 监听支持KVO的属性
//        webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
//        webView.addObserver(self, forKeyPath: "canGoBack", options: .new, context: nil)
//        progressView.setProgress(0.05, animated: true)
//
//        /// 加载URL
//        if let request = webParam.request {
//            webView.load(request)
//        } else if let requestUrl = URL(string: webParam.urlStr) {
//            let request = URLRequest(url: requestUrl)
//            webView.load(request)
//        } else {
//            return
//        }
//
//        /// 设置JS交互
////        p_setJSInteractive()
//    }
//}
//
//// MARK: - WKUIDelegate 不实现该代理方法 网页内调用弹窗时会抛出异常,导致程序崩溃
//extension RRWebShowVC: WKUIDelegate {
//
//    // 获取js 里面的提示
//    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
//
//        let alert = UIAlertController(title: "提示", message: message, preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) -> Void in
//            completionHandler()
//        }))
//        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (_) -> Void in
//            completionHandler()
//        }))
//        self.present(alert, animated: true, completion: nil)
//    }
//
//    // js 信息的交流
//    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
//
//        let alert = UIAlertController(title: "提示", message: message, preferredStyle: .alert)
//        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) -> Void in
//            completionHandler(true)
//        }))
//        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (_) -> Void in
//            completionHandler(false)
//        }))
//        self.present(alert, animated: true, completion: nil)
//    }
//
//    // 交互。可输入的文本。
//    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
//
//        let alert = UIAlertController(title: prompt, message: defaultText, preferredStyle: .alert)
//
//        alert.addTextField { (textField: UITextField) -> Void in
//            textField.textColor = UIColor.red
//        }
//        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) -> Void in
//            completionHandler(alert.textFields![0].text!)
//        }))
//        self.present(alert, animated: true, completion: nil)
//    }
//}
